package com.db.base.impl;

import java.util.List;

import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.db.base.BaseDao;
import com.fasterxml.jackson.databind.ObjectMapper;

@Primary
@Service
@Transactional(rollbackFor = Exception.class)
public class BaseDaoImpl implements BaseDao {
	
	@Autowired
	SqlSessionTemplate sqlSessionTemplate;

	@Override
	public <E> List<E> query(String namespaceId) throws Exception {

		return sqlSessionTemplate.selectList(namespaceId);
	}

	@Override
	public <E> List<E> query(String namespaceId, Object parameters) throws Exception {

		return sqlSessionTemplate.selectList(namespaceId, parameters);
	}

	@Override
	public <T> T queryObject(String namespaceId, Object parameters) throws Exception {

		return sqlSessionTemplate.selectOne(namespaceId, parameters);
	}

	@Override
	public Object insert(String namespaceId, Object parameters) throws Exception {

		return sqlSessionTemplate.insert(namespaceId, parameters);
	}

	@Override
	public <E> void batchInsert(String namespaceId, List<E> parameters) throws Exception {

		SqlSession session = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH, false);
		try {
			int batchCount = 100;// 每批commit的个数
			int count = 0;
			for (int index = 0; index < parameters.size(); index++) {
				session.insert(namespaceId, parameters.get(index));
				if (index > 0 && index % batchCount == 0) {
					count = 0;
					session.commit();
					session.clearCache();
				} else {
					count++;
				}
			}
			if (count > 0) {
				session.commit();
				session.clearCache();
			}
		} finally {
			session.close();
		}
	}

	@Override
	public <E> void batchUpdate(String namespaceId, List<E> parameters) throws Exception {
		SqlSession session = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH, false);
		try {
			int batchCount = 100;// 每批commit的个数
			int count = 0;
			for (int index = 0; index < parameters.size(); index++) {
				session.update(namespaceId, parameters.get(index));
				if (index > 0 && index % batchCount == 0) {
					count = 0;
					session.commit();
					session.clearCache();
				} else {
					count++;
				}
			}
			if (count > 0) {
				session.commit();
				session.clearCache();
			}
		} finally {
			session.close();
		}
	}

	@Override
	public int update(String namespaceId, Object parameters) throws Exception {

		return sqlSessionTemplate.update(namespaceId, parameters);
	}

	@Override
	public int delete(String namespaceId, Object parameters) throws Exception {

		return sqlSessionTemplate.delete(namespaceId, parameters);
	}

	@Override
	public Integer count(String namespaceId) throws Exception {

		return (Integer) this.queryObject(namespaceId);
	}

	@Override
	public Integer count(String namespaceId, Object parameters) throws Exception {

		return (Integer) this.queryObject(namespaceId, parameters);
	}

	@Override
	public Object queryObject(String namespaceId) throws Exception {
		return sqlSessionTemplate.selectOne(namespaceId);
	}

	protected String objectToJson(Object obj) throws Exception {
		if (obj == null)
			return null;
		ObjectMapper mapper = new ObjectMapper();
		return mapper.writeValueAsString(obj);
	}
}
